package org.helldorado.companybuilder;

import org.helldorado.companybuilder.R;

import com.haarman.supertooltips.ToolTip;
import com.haarman.supertooltips.ToolTipRelativeLayout;
import com.haarman.supertooltips.ToolTipView;
import com.haarman.supertooltips.ToolTipView.OnToolTipViewClickedListener;

import android.app.Activity;
import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.style.UpdateAppearance;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class TouchableSpanWithToolTip extends TouchableSpan implements UpdateAppearance     {
	private SpannableStringBuilder mToolTip;
	private View mParentView;
	private int mColor;
	
	public TouchableSpanWithToolTip(SpannableStringBuilder toolTip, View parentView, int color){
		mToolTip = toolTip;
		mParentView = parentView;
		mColor = color;
	}
    /**
     * Performs the touch action associated with this span.
     * @return 
     */
	 @Override
	    public boolean onTouch(View widget, MotionEvent m) {
		  if (m.getAction() == MotionEvent.ACTION_UP)
		  {
			  OnToolTipViewClickedListener myHandlerTT = new OnToolTipViewClickedListener(){  
				  public void onToolTipViewClicked(ToolTipView toolTipView) {
					  toolTipView = null;
				  }
		    	};
		    	ToolTipRelativeLayout mToolTipLayout = (ToolTipRelativeLayout) mParentView.findViewById(R.id.toolTip);

		    	ToolTipView toolTipView = mToolTipLayout.showToolTipForView(
		                new ToolTip()
		                        .withText(mToolTip)
		                        .withColor(mColor)
		                        .withShadow(true)
		                        .withAnimationType(0)
		                        , widget);
		    	int[] pos = new int[]{Math.round(m.getX()), Math.round(m.getY())};
		    	
		    	toolTipView.setOffsetPosition(pos);
		    	ToolTipManager.CloseOpenedToolTip(toolTipView);
		        toolTipView.setOnToolTipViewClickedListener(myHandlerTT);
		  }
       return true;
	    }

    /**
     * Could make the text underlined or change link color.
     */
	 @Override
	    public void updateDrawState(TextPaint ds) {
	        //ds.setUnderlineText(false);
	        //ds.setAntiAlias(true);
	    }

}
